home *** CD-ROM | disk | FTP | other *** search
- Path: news.belwue.de!uzwil!kuehl
- From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ Gurus! Is it correct?
- Date: 15 Apr 1996 21:55:41 GMT
- Organization: FakultΣt fⁿr Mathematik und Informatik
- Message-ID: <4kugkt$j3s@news.BelWue.DE>
- References: <4eqvtg$cg5@israel-info.datasrv.co.il> <4etnju$6gn@rolaids.frco.com> <4f0n8s$a3b@news2.ios.com> <311403bf.169980736@nntp.ix.netcom.com> <4f2koo$q5a@news2.ios.com>
- Reply-To: dietmar.kuehl@uni-konstanz.de
- NNTP-Posting-Host: uzwil.informatik.uni-konstanz.de
- X-Newsreader: TIN [version 1.2 PL2]
-
- Hi,
-
- Vlastimil Adamovsky (vlad@gramercy.ios.com) wrote:
- : miker3@ix.netcom.com (Mike Rubenstein) wrote:
-
- : >vlad@gramercy.ios.com (Vlastimil Adamovsky) wrote:
- : >> I don't think the virtual destructor is necessary in this specific
- : >> case where you have no added data in subclasses.
-
- : >Why do you think having data has anything to do with it? From draft
- : >5.3.5:
-
- : > In the first alternative (delete object), if the static type
- : > of the operand is different from its dynamic type, the static
- : > type shall be a base class of the operand's dynamic type
- : > and the static type shall have a virtual destructor or the
- : > behavior is undefined.
-
-
- : I prefer slightly more simple explanation as follows:
- : (The Design and Evolution of C++, chapter 10.5 page 216)
- : ....
- : Had a virtual destructor not been used, the cleanup specified in
- : Y's (in our case class B) destructor would not have been performed.
-
- : Here you see you have choice to use virtual destructor or not.
-
- Although the D&E is written by the inventor of C++ it does not define
- the standard. The standard is very clear what is about to happen: If an
- object of a derived class is deleted through a pointer to the base
- class the base class has to have a virtual destructor, otherwise the
- behavior is undefined. This means, the code posted has undefined
- behavior. I would call code with undefined behavior to be incorrect.
-
- Although the code is likely to work with most implementations, the code
- could break (even without any modification) if the memory allocation
- procedure uses a simple optimization: For objects with size 1 a buffer
- is used to avoid multiple calls to the OS's memory handling system. For
- larger objects, the normal memory allocation is used. The deallocation
- function is then told the size of the deleted object from its static
- size (if there is no virtual destructor) or from its dynamic size (if
- there is a virtual destructor). Although such an implementation cannot
- be built by a library it can be built by the C++ implementation (the
- "normal" global 'operator new()' doesn't get the size of the object;
- however, the C++ implementation is free on what to pass to the built-in
- 'operator new()')!
-
- All which remains is to check is that 'sizeof(A) == 1' and 'sizeof(C) ==
- 2' for some implementations (e.g. gcc-2.7.2): You would get some weird
- error like an segmentation fault when deleting a 'C' through a pointer
- to an 'A'.
- --
- dietmar.kuehl@uni-konstanz.de
- http://www.informatik.uni-konstanz.de/~kuehl/
- I am a realistic optimist - that's why I appear to be slightly pessimistic
-